﻿//
// Copyright (c) 2011 Canyala Innovation AB
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
// associated documentation files (the "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH 
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
//

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;

namespace Canyala.Lagoon.Extensions
{
    public static class StringExtensions
    {
        public static string Args(this string format, params object[] arguments)
        { 
            Contract.Requires(format != null);
            Contract.Requires(arguments != null);

            return String.Format(format, arguments); 
        }

        /// <summary>
        /// Extension for strings that is a shortcut to create formatted strings with a 
        /// format provider.
        /// </summary>
        /// <param name="format">The string format expression.</param>
        /// <param name="formatProvider">A format provider to use when formatting text.</param>
        /// <param name="arguments">The arguments to format.</param>
        /// <returns>A formatted string with the arguments.</returns>
        /// <remarks>
        /// This extension is a short form for the <see cref="String.Format"/>
        /// method. It allows you to write, for example:
        /// <code>
        /// "{0}, {1}, {2}".Args(CultureInfo.CurrentCulture, 1, 2, 3)   // => "1, 2, 3"
        /// </code>
        /// Instead of:
        /// <code>
        /// String.Format(CultureInfo.CurrentCulture, "{0}, {1}, {2}", 1, 2, 3);
        /// </code>
        /// </remarks>
        public static string Args(this string format, IFormatProvider formatProvider, params object[] arguments)
        {
            Contract.Requires(format != null);
            Contract.Requires(formatProvider != null);
            Contract.Requires(arguments != null);

            return string.Format(formatProvider, format, arguments);
        }

        public static string Join(this IEnumerable<string> seqOfStrings, char separator)
        {
            Contract.Requires<ArgumentException>(seqOfStrings != null);

            var builder = new StringBuilder();

            foreach (var stringItem in seqOfStrings)
            {
                if (builder.Length > 0) builder.Append(separator);
                builder.Append(stringItem);
            }

            return builder.ToString();
        }

        public static string Join(this IEnumerable<string> seqOfStrings, string separator)
        {
            Contract.Requires<ArgumentException>(seqOfStrings != null);

            var builder = new StringBuilder();

            foreach (var stringItem in seqOfStrings)
            {
                if (builder.Length > 0) builder.Append(separator);
                builder.Append(stringItem);
            }

            return builder.ToString();
        }

    }
}
